import java.awt.*;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Scanner;

/**
 * BiliBili视频自动切换标签刷新播放量
 */
public class BiliBiliRefreshTheVideo {

    public static void main(String[] args) {
        int count = 1; // 运行次数，默认10次
        int videoTime = 6; // 视频最长时间，默认6分钟
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入运行次数，不输入则默认为1次(直接按回车)：");
        String str = scanner.nextLine();
        if (!Objects.equals(str, "")) count = Integer.parseInt(str);
        System.out.println("请输入单个视频最长的时间（分钟，例：2分2秒输入3），不输入则默认为6分钟(直接按回车)：");
        str = scanner.nextLine();
        if (!Objects.equals(str, "")) videoTime = Integer.parseInt(str);
        // 创建机器人
        try {
            Robot robot = new Robot();
            int[] number = new int[]{
                    KeyEvent.VK_0, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4,
                    KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8, KeyEvent.VK_9
            };
            // 等待
            System.err.println("正在启动...\n5秒后开始运行...");
            Thread.sleep(100);
            for (int i = 0; i < 5; i++) {
                System.out.print("***");
                Thread.sleep(1000);
            }
            long theCurrentTime = System.currentTimeMillis();
            // 运行次数为1次，则计算9个标签的延迟时间
            Date date = new Date(theCurrentTime + (long) videoTime * (count - 1) * (60 * 1000 + 500 * 9) + 6000);
            String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            System.err.println("\n开始运行...\n结束时间：" + endTime);
            Thread.sleep(100);
            for (int j = 0; j < count; j++) {
                // 移动鼠标 定位到浏览器
                robot.mouseMove(500, 500);
                // 按下中键并松开
                robot.mousePress(KeyEvent.BUTTON2_DOWN_MASK);
                // 松开中键
                robot.mouseRelease(KeyEvent.BUTTON2_DOWN_MASK);
                // 自动化执行 浏览器标签1-9
                System.out.println("第" + (j + 1) + "次运行开始");
                for (int i = 0; i < 9; i++) {
                    TheRefresh(robot, number[i + 1]);
                    robot.delay(500);
                    // 按f5
                    robot.keyPress(KeyEvent.VK_F5);
                    robot.keyRelease(KeyEvent.VK_F5);
                }
                // 休息6分钟
                if (j < count - 1) {
                    theCurrentTime = System.currentTimeMillis();
                    date = new Date(theCurrentTime + (long) videoTime * 60 * 1000 + 1000 * 9); // 9个标签的延迟
                    String nextTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                    System.out.println("第" + (j + 1) + "次运行完成\n下一次运行时间：：" + nextTime);
                    Thread.sleep((long) videoTime * 60 * 1000 + 1000 * 9);
                }
            }
        } catch (AWTException | InterruptedException e) {
            System.out.println(e.getMessage());
        }
        System.err.println("\n运行结束...");
    }

    /**
     * 按下ctrl+"number"键，并F5刷新
     * @param robot  Robot对象
     * @param number 数字
     */
    public static void TheRefresh(Robot robot, Integer number) {
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(number);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(number);
        System.out.println("按下ctrl + " + (number - '0'));
    }

}
